package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.http.JsonTools;
import com.diudiutechnology.www.yadiudiu.util.utils.Constant;
import com.diudiutechnology.www.yadiudiu.util.utils.Dialog;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.BaseActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.LoginActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.RegistrationModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.ShippingAddressModel;
import com.diudiutechnology.www.yadiudiu.R;

import org.xutils.common.Callback;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/22.
 * 0、添加地址 1、修改地址
 */

public class AddShippingAddressActivity extends BaseActivity implements View.OnClickListener ,Callback.CommonCallback<String>{
    @Bind(R.id.retur)
    LinearLayout retur;
    @Bind(R.id.delete)
    LinearLayout delete;

    @Bind(R.id.addres_layout)//地址
            LinearLayout addres_layout;

    @Bind(R.id.community)//小区
            TextView community;
    @Bind(R.id.plot_details)//小区详情
            TextView plot_details;


    @Bind(R.id.title_center)
    TextView title_center;

    @Bind(R.id.admin_name)//姓名
            EditText admin_name;


    @Bind(R.id.phone)//手机号
    EditText phone;
    @Bind(R.id.detailed)//详情
            EditText detailed;


    @Bind(R.id.nar_menu)//男女
            RadioGroup nar_menu;

    @Bind(R.id.button)//添加
            Button button;

    Context context;

    String pos = "0";//0、添加地址 1、修改地址

    int number=0;
    List<ShippingAddressModel> shippingAddress;
    String posid = "";
    String sex="2";
    String province ="";//省
    String city   ="";// 市
    String area ="";//  区
    String longitude ="";//   经度
    String latitude  ="";//  纬度

    private MyProgressDialog mDialog;
    AlertDialog alertDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_shipping_address);
        ButterKnife.bind(this);
        context = this;
        initView();
    }

    private void initView() {
        retur.setOnClickListener(this);
        addres_layout.setOnClickListener(this);
        button.setOnClickListener(this);
        delete.setOnClickListener(this);
        ((RadioButton) nar_menu.getChildAt(0)).setChecked(true);
        if (getIntent() != null) {
            pos = getIntent().getStringExtra("pos");
            if (pos.equals("0")) {
                delete.setVisibility(View.GONE);
                button.setText("添加");
            } else {
                posid = getIntent().getStringExtra("id");
                title_center.setText("修改地址");
                button.setText("修改");
                number=getIntent().getIntExtra("number",0);
                shippingAddress= (List<ShippingAddressModel>) getIntent().getSerializableExtra("data");
            }
        }
        ininData();
        nar_menu.setOnCheckedChangeListener(checkedChangeListener);
    }
    private void ininData(){
        if (pos.equals("1")){
            ShippingAddressModel shippingAddressModel = shippingAddress.get(number);


            if (!Sundry.getStinges(shippingAddressModel.getDetailedadd())){
                String[] split = shippingAddressModel.getDetailedadd().split(",");
                for (int i=0;i<split.length;i++){
                    if (i==0){
                        community.setText(split[0]);
                    }
                    if (i==1){
                        plot_details.setText(split[1]);
                    }
                    if (i==2){
                        detailed.setText(split[2]);
                    }

                }
            }
            sex=shippingAddressModel.getSex();
            if (shippingAddressModel.getSex().equals("2")){
                ((RadioButton) nar_menu.getChildAt(0)).setChecked(true);
            }else {
                ((RadioButton) nar_menu.getChildAt(1)).setChecked(true);
            }
            admin_name.setText(shippingAddressModel.getConsignee());
            phone.setText(shippingAddressModel.getUserphone());
            province =shippingAddressModel.getProvince();//省
            city   =shippingAddressModel.getCity();// 市
            area =shippingAddressModel.getArea();//  区
            longitude   =shippingAddressModel.getLongitude();
            latitude =shippingAddressModel.getLatitude();
        }
    }
    /**
     * RadioGroup点击事件
     */
    private RadioGroup.OnCheckedChangeListener checkedChangeListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                case R.id.electric:
                    sex="2";

                    break;
                case R.id.tricycle:
                    sex="3";
                    break;
            }
        }
    };
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
            case R.id.delete:
                alertDialog= Dialog.phonedialog(context, "确认删除地址", "删除地址", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        processdelete();
                        alertDialog.dismiss();
                    }
                },"确定","取消");
                break;

            case R.id.addres_layout:
                Intent intent = new Intent(context, SelectTheAddressActivity.class);
                intent.putExtra("pos","0");
                startActivityForResult(intent, Constant.ADDRESS);
                break;
            case R.id.button:
                if (TextUtils.isEmpty(admin_name.getText().toString().trim())){//姓名
                    ToastUtils.show(context, getResources().getString(R.string.please_enter_name));
                    return;
                }
                if (TextUtils.isEmpty(phone.getText().toString().trim())){//手机号
                    ToastUtils.show(context, getResources().getString(R.string.please_phone_number));
                    return;
                }
                if (!Sundry.isMobileNO(phone.getText().toString().trim())){//验证手机号
                    ToastUtils.show(context, getResources().getString(R.string.please_correctness_phone_number));
                    return;
                }
                if (TextUtils.isEmpty(community.getText().toString().trim())){//小区
                    ToastUtils.show(context, getResources().getString(R.string.please_community));
                    return;
                }
                    processadd();
                break;
        }
    }
    //添加
    private void processadd(){
        if (Sundry.isOpenNetwork(context)){
            mDialog =new MyProgressDialog(context);
            String str=community.getText().toString().trim()+","+plot_details.getText().toString().trim()+","+detailed.getText().toString().trim();
            if (pos.equals("0")){//添加地址
             HttpClient.addUserAddress(admin_name.getText().toString().trim(),sex,phone.getText().toString().trim(),province,city,area,
                     str,longitude,latitude,this);
            }else {//修改

                HttpClient.editUserAddress(shippingAddress.get(number).getId(),admin_name.getText().toString().trim(),sex,phone.getText().toString().trim(),province,city,area,
                        str,longitude,latitude,this);
            }
        }else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }
    //删除
    private void processdelete(){
        if (Sundry.isOpenNetwork(context)){
            mDialog =new MyProgressDialog(context);
            HttpClient.deleDeliveryAddress(shippingAddress.get(number).getId(),this);

        }else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }
    @Override
    public void onSuccess(String result) {
        if (mDialog!=null){
            mDialog.dismiss();
        }
        System.out.println("stringmd5result"+result);
        List<RegistrationModel> getnote = JsonTools.getunderlying(result);
        if (getnote!=null||getnote.size()>0){
            String status = getnote.get(0).getStatus();
            String getMsg = getnote.get(0).getMsg();
            if (status.equals("200")){

                ToastUtils.show(context, getMsg);
                finish();
            }else if (status.equals("105")){
                Intent intent = new Intent(context, LoginActivity.class);
                startActivity(intent);
                ToastUtils.show(context, getMsg);
            }else {
                ToastUtils.show(context, getMsg);
            }
        }
    }
    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        if (mDialog!=null){
            mDialog.dismiss();
        }
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onCancelled(CancelledException cex) {
        if (mDialog!=null){
            mDialog.dismiss();
        }
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onFinished() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        System.out.println("stringmd5="+requestCode+"resultCode="+resultCode);
        if (resultCode==Constant.ADDRESS){
            if (requestCode==Constant.ADDRESS){
                community.setText(data.getStringExtra("name"));
                plot_details.setText(data.getStringExtra("address"));
                province =data.getStringExtra("province");//省
                city   =data.getStringExtra("city");// 市
                area =data.getStringExtra("district");//  区
                longitude =data.getStringExtra("longitude");//   经度
                latitude  =data.getStringExtra("latitude");//  纬度
            }
        }
    }
}
